#!/bin/bash
set -e

USER=djwatch
CURDIR=`pwd`

echo "Create user ${USER}"
grep -s -q "^${USER}:" /etc/passwd || useradd --system ${USER}
mkdir -p /etc/djwatchd/plugins
mkdir -p /etc/djwatchd/utils
ln -sfv $CURDIR/etc_djwatchd/plugins/* /etc/djwatchd/plugins/
ln -sfv $CURDIR/../utils/* /etc/djwatchd/utils/
ln -sfv $CURDIR/etc_djwatchd/settings.py /etc/djwatchd/
ln -sfv $CURDIR/djwatchd.py /usr/bin/djwatchd
ln -sfv $CURDIR/djwatchgrabberd.py /usr/bin/djwatchgrabberd
chmod +x /usr/bin/djwatchd /usr/bin/djwatchgrabberd

echo 'Autostart'
if [ -d /etc/supervisor/conf.d ]; then
    ln -sf $CURDIR/supervisord.conf /etc/supervisor/conf.d/djwatch.conf
    /etc/init.d/supervisor stop
    /etc/init.d/supervisor start
else
    echo 'Supervisor not found. Make start manually or (for ubuntu) install supervisord (http://supervisord.org/):'
    echo 'sudo apt-get install supervisor'
fi
